package com.qc.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;

public class AlipayUtil {

	public static String gatewayUrl = "https://openapi.alipay.com/gateway.do";// 自己的应用网关
	public static String APP_ID = "2017010404839381";
	public static String SELLER_ID = "2088421458871750";// 卖家支付宝账户
	public static String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC6ASuUs1+z4Z1O7JjsdzdlAqAdx49NF6j6WClfse+wmd3xzUCUyEYKIOBguhI9rjzEj1DMWRd+yk0F3JEsso2uKLClFDsp7A/LS4J6xMSxzlhcG32ZUPLxxvZA7VpwfmDminNN45KGHczrSPK5F0RO7t4X+bYZdZq4cQ5h2I+6DtJr09t9r9Zp7XXOvuoU/9UrgLi15QVi3zzYxLQjpOq76c8O0bdqTtzTpxVHVFoOzWa+5ObLuCgIPk9MnPFVCZYE2Jj2hYcJePzXo9Xrven6IXBgLB310BfvSeaIPf3zI+8Qho7mFdnqJIix1KxcqB2JAeSWuanj5iK2V5pjryg/AgMBAAECggEAfnUFOBerXRRqapoYbcuvcZETYqumFXecfncpydGjb6Fsb1RoL77pP0GSyrA2bpDi54+4EvYPLzhCnVWTrZvHDzzCu7jBcIL5SfGQR/826tuq3xQhk/ag9RebnBDCh/Cj2JkQQ0gQ+fWQt/Xkb0oOOsvTWuF+mFiIVBUo/gTJ0mfQCu6+B40h1S5tCjIBuirWekTIlwJ92PKasSQJuRRgHRcHVwhMuyGb/QJJhy3bxtlKIY1730rFkOl5WmlxVoS7hmvz8LYN9MQGKavuBIWSaYFLqRPjB7ldw8b0oL3akffJtV5tyg0abxJDuzpZHK13PCIFOCkKfaLwV4DFA+5GYQKBgQDn86etLI8E+6+KS0KYIxT9OOxGY3C9J9ylPFbpBQfhPOBzDxG2Ca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tqu+nPDtG3ak7c06cVR1RaDs1mvuTmy7goCD5PTJzxVQmWBNiY9oWHCXj816PV673p+iFwYCwd9dAX70nmiD398yPvEIaO5hXZ6iSIsdSsXKgdiQHklrmp4+YitleaY68oPwIDAQAB";
	public static String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
	public static String CHARSET = "utf-8";

	public static String getAlipaySign(String content) throws Exception {
		// AlipayClient alipayClient = new
		// DefaultAlipayClient(gatewayUrl,APP_ID,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,"RSA");
		// System.out.println("签名前:"+content);
		String signStr = AlipaySignature.rsaSign(content, APP_PRIVATE_KEY, CHARSET);
		// System.out.println("签名后:"+signStr);
		return signStr;
	}

	/**
	 * 退款
	 * 
	 * @param orderNumber
	 *            订单号
	 * @param amount
	 *            金额
	 * @throws AlipayApiException
	 */
	public static boolean refund(String orderNumber, double amount) throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET,
				ALIPAY_PUBLIC_KEY, "RSA");
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		String str1 = "{\"out_trade_no\":\"" + orderNumber + "\",\"refund_amount\":" + amount + "}";
		request.setBizContent(str1);
		AlipayTradeRefundResponse response = alipayClient.execute(request);
		if (response.isSuccess()) {
			System.out.println("调用成功");
			return true;
		} else {
			System.out.println("调用失败");
			return false;
		}
	}

	public static void main(String[] args) {
		try {
			refund("20170223633922", 0.01);
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// try {

		// 实例化客户端
		// AlipayClient alipayClient = new
		// DefaultAlipayClient(gatewayUrlTest,APP_ID,APP_PRIVATE_KEY,"json","UTF-8",ALIPAY_PUBLIC_KEY,"RSA");
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify
		// wyw: alipay.trade.app.pay
		// AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		// SDK已经封装掉了公共参数，这里只需要传入业务参数
		// 此次只是参数展示，未进行字符串转义，实际情况下请转义
		// request.setBizContent("{\"timeout_express\":\"30m\",\"seller_id\":\"\",\"product_code\":\"QUICK_MSECURITY_PAY\",\"total_amount\":\"0.01\",\"subject\":\"1\",\"body\":\"我是测试数据\",\"out_trade_no\":\"IQJZSRC1YMQB5HU\"}");
		// AlipayTradeAppPayResponse response = alipayClient.execute(request);
		// //调用成功，则处理业务逻辑
		// if(response.isSuccess()){
		// System.out.println("===="+response);
		// }
		// System.out.println(AlipaySignature.rsaSign("{\"A\":\"vv\"}",
		// APP_PRIVATE_KEY, "UTF-8"));
		// } catch (AlipayApiException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}
}
